package com.schoolCommunity.util;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class CheckEmailPass {

    @Autowired
    @Qualifier("MyLoginTemplate")
    private RedisTemplate redisTemplate;

    static final String EmailSignIn = "EmailSignIn";

    static final String EmailGetPass = "EmailGetPass";

    //传入用户email和验证码检验，登录用的验证码
    public String checkEmailSignIn(String memberEmail, String memberEmailPass) {

        Logger log = Logger.getLogger(CheckEmailPass.class);
        //先判断redis里有没有此验证码
        Boolean aBoolean = redisTemplate.hasKey(memberEmail + EmailSignIn);

        if (aBoolean) { //验证码存在
            log.info("注册用验证码存在于redis中，即将进行判断");

            //与缓存比较验证码是否正确
            if (redisTemplate.opsForValue().get(memberEmail + EmailSignIn).toString().equals(memberEmailPass)) {
                log.info("验证码与缓存匹配正确");
                return "验证码匹配";
            } else {
                log.info("验证码匹配错误");
                return "验证码错误";
            }

        } else {

            return "验证码不存在或已过期";

        }

    }

    //传入用户email和验证码检验，登录用的验证码
    public String checkChangePass(String memberEmail, String memberEmailPass) {

        Boolean aBoolean = redisTemplate.hasKey(memberEmail + EmailGetPass);

        if (aBoolean) { //验证码存在

            //与缓存比较验证码是否正确
            if (redisTemplate.opsForValue().get(memberEmail + EmailGetPass).toString().equals(memberEmailPass)) {
                return "验证码匹配";
            } else {
                return "验证码错误";
            }

        } else {

            return "验证码不存在或已过期";

        }

    }


}
